//
//  CampusDetailVideoItemCell.m
//  ShowYork
//
//  Created by hxh on 2021/3/26.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusDetailVideoPreviewCell.h"
#import "CapusDetailVideoItemCell.h"
@interface CampusDetailVideoPreviewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,copy)NSArray *dataArray;
@end

@implementation CampusDetailVideoPreviewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}


- (void)setUI{
    
    NSLog(@"%f", KScaleHeight(270));
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsZero;
    layout.itemSize = CGSizeMake(KScaleHeight(270)/16*9, KScaleHeight(270));
    layout.minimumLineSpacing = 8.0f;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 20);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:self.collectionView];
    
    [self.collectionView registerClass:[CapusDetailVideoItemCell class] forCellWithReuseIdentifier:@"CapusDetailVideoItemCell"];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(20);
        make.bottom.equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(KScaleHeight(270)).priorityHigh();
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView);
    }];
}
- (void)setVlogList:(NSArray *)vlogList{
    self.dataArray = vlogList;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CapusDetailVideoItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CapusDetailVideoItemCell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CampusSchoolVideoItemModel *item = self.dataArray[indexPath.item];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(playSchoolVlogByItem:)]) {
        [self.delegate playSchoolVlogByItem:item];
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"CampusDetailVideoPreviewCell";
    CampusDetailVideoPreviewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CampusDetailVideoPreviewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

@end
